package com.foreknow.demo18;

import java.sql.*;

/**
 * 预处理 PreparedStatement接口
 * 什么是预处理：就是我们的SQL语句中有？(表示占位符)
 * Statement:它只能执行静态的SQL语句  例如:insert into emp(empno,ename,job) values(9999,'fender','music')
 * PreparedStatement:可以执行动态的SQL语句 例如：insert into emp(empno,ename,job) values(?,?,?)
 */

public class JdbcPreparedStatement {
    public static void main(String[] args) {
        try { Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/basic2","root","123456");
            //获取statement对象
            PreparedStatement stmt =conn.prepareStatement("insert into  emp(empno,ename,job)values(?,?,?)");
            //一定要将? 替换一个具体的值
//            void setFloat(int parameterIndex,float x)parameterIndex表示第几个值?  x表示替换的值
            stmt.setInt(1,8888);
            stmt.setString(2,"tomm");
            stmt.setString(3,"music");
            int isRligth =stmt.executeUpdate();
            System.out.println(isRligth);

            stmt.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
